/* Copyright 2006 Nicklas R Johnson
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package net.spatula.tally_ho.ui.article.read;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import net.spatula.tally_ho.service.ArticleService;
import net.spatula.tally_ho.service.beans.ArticleBean;
import net.spatula.tally_ho.service.beans.UserBean;
import net.spatula.tally_ho.ui.WicketUtils;
import net.spatula.tally_ho.ui.signin.SignIn;
import net.spatula.tally_ho.ui.signin.Utils;
import wicket.Page;
import wicket.markup.html.basic.Label;
import wicket.markup.html.link.ExternalLink;
import wicket.markup.html.link.Link;
import wicket.markup.html.link.PageLink;
import wicket.markup.html.list.ListItem;
import wicket.markup.html.list.ListView;
import wicket.markup.html.panel.Panel;
import wicket.resource.IPropertiesFactory;
import wicket.resource.Properties;

/**
 * @author spatula
 * 
 */
public class ActionsPanel extends Panel {

    private static final long serialVersionUID = 1L;

    private Map<String, Visitable> actionMapping = new HashMap<String, Visitable>();

    public ActionsPanel(String arg0, ArticleBean article) {
        super(arg0);

        UserBean user = Utils.getUserBean();
        
        Locale defaultLocale = getApplication().getResourceSettings().getDefaultLocale();
        IPropertiesFactory props = getApplication().getResourceSettings().getPropertiesFactory();
        Properties properties = props.get(getApplication(), ActionsPanel.class, null, defaultLocale);
        
        String updateServlet = properties.getString("updateArticleServlet");
        actionMapping.put("Update", new ActionExternalLink(updateServlet+ "?id=" + article.getId(), "Update this article"));
        actionMapping.put("Delete", new ActionLink(ArticleRead.class, "Delete this article"));

        List<Visitable> links = new ArrayList<Visitable>();

        if (user == null) {
            ActionLink loginLink = new ActionLink(new SignIn(WicketUtils.getMyUrl()), "Log in if you are "
                    + article.getCreator().getUsername() + " for more options.");
            links.add(loginLink);
        } else {

            List<String> actions = ArticleService.getInstance().getActions(article, user);

            for (String action : actions) {
                links.add(actionMapping.get(action));
            }
        }
        add(new ActionsListView("actions", links));
        
    }

    private interface Visitable {
        public void accept(Visitor visitor, ListItem item);
    }
    
    private interface Visitor {
        void visit(ActionLink link, ListItem item);
        void visit(ActionExternalLink link, ListItem item);
    }
    
    private class ActionExternalLink implements Serializable, Visitable {
        private static final long serialVersionUID = 1L;
        private ExternalLink pageLink;
        private Label linkText;
        
        private ActionExternalLink(final String url, String linkText) {
            this.pageLink = new ExternalLink("action", url);
            this.linkText = new Label("actionText", linkText);
        }

        public void accept(Visitor visitor, ListItem item) {
            visitor.visit(this, item);
        }
        
        public Label getLinkText() {
            return linkText;
        }

        
        public void setLinkText(Label linkText) {
            this.linkText = linkText;
        }

        
        public ExternalLink getPageLink() {
            return pageLink;
        }

        
        public void setPageLink(ExternalLink pageLink) {
            this.pageLink = pageLink;
        }
        
        

    }
    
    private class ActionLink implements Serializable, Visitable {

        private static final long serialVersionUID = 1L;
        private Link pageLink;
        private Label linkText;

        private ActionLink(Class pageClass, String linkText) {
            this.pageLink = new PageLink("action", pageClass);
            this.linkText = new Label("actionText", linkText);
        }
 
        private ActionLink(Page page, String linkText) {
            this.pageLink = new PageLink("action", page);
            this.linkText = new Label("actionText", linkText);
        }
        
        public void accept(Visitor visitor, ListItem item) {
            visitor.visit(this, item);
        }

        public Label getLinkText() {
            return linkText;
        }

        public Link getPageLink() {
            return pageLink;
        }

    }

    private class ActionsListView extends ListView implements Visitor {

        private static final long serialVersionUID = 1L;

        public ActionsListView(String arg0, List arg1) {
            super(arg0, arg1);
        }

        public void visit (ActionLink link, ListItem item) {
            item.add(link.getPageLink().add(link.getLinkText()));
        }
        
        public void visit(ActionExternalLink link, ListItem item) {
            item.add(link.getPageLink().add(link.getLinkText()));
        }
        
        @Override
        protected void populateItem(ListItem item) {
            
            Visitable link = (Visitable) item.getModelObject();
            link.accept(this, item);
            
            

        }

    }

}
